package lolbatfsm.implementation.gui;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.util.Collection;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import lolbatfsm.system.FSM;

/**
 * This class is a form that show the iteration results.<br>
 * To do this it uses a JScrollPanel with a GridBagLayout,
 * so it doesn't matter how many FSMs you pass the class,<br>
 * you'll be able to scroll them.<br>
 * There are 2 buttons: clicking on Next you'll start the
 * simulation, while clicking Cancel you'll stop the simulation.<br>
 *
 *
 * @author Fabio Lola <fadan87@gmail.com>
 */
public class IterationGUI extends javax.swing.JDialog {

    //private JButton okButton = new JButton("Next");
    private final int iterationDuration;
    private final Collection<FSM> fsms;
    private boolean next = true;
    private final int totalDuration;

    static IterationGUI createAndShow(int iterationDuration, Collection<FSM> fsms, int totalDuration) {
        IterationGUI iterationGUI = new IterationGUI(iterationDuration, fsms, totalDuration);
        iterationGUI.setLocation(200, 100);
        iterationGUI.setModal(true);
        iterationGUI.setVisible(true);
        return iterationGUI;
    }

    static IterationGUI createAndShowTest(int iterationDuration, Collection<FSM> fsms, int totalDuration) {
        IterationGUI iterationGUI = new IterationGUI(iterationDuration, fsms, totalDuration);
        iterationGUI.setLocation(200, 100);
        iterationGUI.setVisible(true);
        return iterationGUI;
    }
    
    /**
     * @param iterationDuration The iteration's duration in int
     * @param fsms Collection of FSMs that will be shown
     */
    private IterationGUI(int iterationDuration, Collection<FSM> fsms, int totalDuration) {
        initComponents();
        this.totalDuration = totalDuration;
        this.iterationDuration = iterationDuration;
        this.fsms = fsms;
        GridBagConstraints c = new GridBagConstraints();
        GridBagLayout gridBagLayout = new GridBagLayout();
        panel.setLayout(gridBagLayout);
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.weightx = 0.5;
        c.weighty = 0.1;
        c.insets = new Insets(5, 5, 5, 0);
        c.gridx = 0;
        JLabel iterationDurationLabel = new JLabel("Iteration duration:   " + iterationDuration);
        iterationDurationLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
        panel.add(iterationDurationLabel, c);
        c.gridx++;
        JLabel totalDurationLabel = new JLabel("Total duration from start:   " + totalDuration);
        totalDurationLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
        panel.add(totalDurationLabel, c);
        c.gridy++;
        c.gridx = 0;
        JLabel fsmNameLabel = new JLabel("FSM name");
        fsmNameLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
        panel.add(fsmNameLabel, c);
        c.gridx++;
        JLabel currentStateLabel = new JLabel("Current state");
        currentStateLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
        panel.add(currentStateLabel, c);
        c.gridx++;
        JLabel pendingTransitionsLabel = new JLabel("Pending transitions");
        pendingTransitionsLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
        panel.add(pendingTransitionsLabel, c);
        c.gridy++;
        for (FSM fsm : fsms) {
            c.gridx = 0;
            panel.add(new JLabel(fsm.getName()), c);
            c.gridx++;
            panel.add(new JLabel(fsm.getCurrentState().getName()), c);
            c.gridx++;
            if (fsm.hasPendingTransition()) {
                panel.add(new JLabel(fsm.getPendingTransition().toString()), c);
            } else {
                panel.add(new JLabel(""), c);
            }
            c.gridy++;
        }
        c.weighty = 1.0;
        panel.add(Box.createVerticalGlue(), c);
        panel.repaint();
        panel.validate();
        this.pack();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        panel = new javax.swing.JPanel();
        cancelButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(null);
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));

        jScrollPane1.setBorder(null);

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 667, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 302, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(panel);

        cancelButton.setText("Stop Simulation");
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });

        nextButton.setText("Next");
        nextButton.setName("nextButton"); // NOI18N
        nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nextButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 667, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(nextButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nextButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextButtonMouseClicked
        next = true;
        this.setVisible(false);
    }//GEN-LAST:event_nextButtonMouseClicked

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseClicked
        next = false;
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonMouseClicked

    /**
     * Return a boolean saying if the simulation has to continue or not
     * @return next Boolean true continue, false stop simulation
     */
    public boolean isSimulationStopped() {
        return !next;
    }

    public void testNextButtonClick(){
        nextButtonMouseClicked(new MouseEvent(cancelButton, 0, 0, 0, 0, 0, 1, false));
    }

    public void testCancelButtonClick(){
        cancelButtonMouseClicked(new MouseEvent(cancelButton, 0, 0, 0, 0, 0, 1, false));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nextButton;
    private javax.swing.JPanel panel;
    // End of variables declaration//GEN-END:variables
}
